package com.tianhua.javs.core.func.translate.dataobject;

import com.tianhua.javs.client.bean.JavsParamBean;
import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.func.IBusinessTranslatorFunc;
import com.tianhua.javs.client.model.MethodModel;
import com.tianhua.javs.core.annotations.Segment;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.func.translate.AbstractTranslator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description: bo类的转换信息 转换到do
 * date: 2022/2/17
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Slf4j
@Component(value = "convertboFunc")
@Segment(tag = "convertbo(),convertbos()",scriptFuncBean = "doFunc",funcTag = "do")
public class ConvertBO extends AbstractTranslator implements IBusinessTranslatorFunc {

    @Override
    public JavsSegmentBean translate(String shellSegment, Map<String, JavsParamBean> javsParamBeanMap) {
        if (shellSegment.endsWith("convertbo()")) {
            String varName = shellSegment.split("\\.")[0].trim();

            JavsParamBean convertParamBean = javsParamBeanMap.get(shellSegment.trim());
            if (convertParamBean == null){
                return JavsSegmentBean.getInstance(shellSegment);
            }

            JavsParamBean varParamBean = javsParamBeanMap.get(varName);

            MethodModel methodModel = convertParamBean.getReferClass().findMethod(varParamBean.getParamType(),"bo",1);
            if(methodModel != null){
                StringBuilder builder = new StringBuilder();
                builder.append(methodModel.getReturnClass());
                builder.append(TagConstants.BLANK_ONE);
                String javaVarName = methodModel.getReturnClass().substring(0,1).toLowerCase() + methodModel.getReturnClass().substring(1);
                builder.append(javaVarName);
                builder.append(TagConstants.BLANK_ONE);
                builder.append(TagConstants.EQ);
                builder.append(TagConstants.BLANK_ONE);
                builder.append(convertParamBean.getParamType());
                builder.append(TagConstants.POINT);
                builder.append("getInstance()");
                builder.append(TagConstants.POINT);
                builder.append(methodModel.getMethdSimpleName());
                builder.append(TagConstants.BRACKET_LEFT);
                builder.append(varParamBean.getOriginParamName());
                builder.append(TagConstants.BRACKET_RIGHT);

                ScriptVarMappingBean scriptVarMappingBean = ScriptVarMappingBean.builder()
                        .referClassModel(convertParamBean.getReferClass())
                        .varClassName(methodModel.getReturnClass()).build();


                JavsSegmentBean javsSegmentBean = JavsSegmentBean.getMatchInstance(builder.toString(), methodModel.getReturnClass(), varParamBean.getPackageName());
                javsSegmentBean.setScriptVarMappingBean(scriptVarMappingBean);
                return javsSegmentBean;
            }else {
                log.warn("method not find, paramType = {},classTag = {},paramCount = {}",varParamBean.getParamType(),"do",1);
            }
        }

        return JavsSegmentBean.getInstance(shellSegment);
    }
}
